package com.thunbu.thunbu_meeting_system.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class RetrofitTransUtil{
    public static  MultipartBody.Part file2Part(File file){
        return file2Part("file",file);
    }

    public static  MultipartBody.Part file2Part(String name,File file){
        RequestBody bodySingle = RequestBody.create(MultipartBody.FORM,file);
        final   MultipartBody.Part bodyPart = MultipartBody.Part.createFormData(
                name, file.getName(),bodySingle);
        return bodyPart;
    }

    public static RequestBody string2RequestBody(String param){
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), param);
        return requestBody;
    }
    public static  Map<String,RequestBody> map2RequestBody(Map<String,Object> params){
        Map<String,RequestBody> map = new HashMap<String, RequestBody>();
        if (params!= null && !params.isEmpty()) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() != null) {
                    if (entry.getValue() instanceof String) {
                        map.put(entry.getKey(), RetrofitTransUtil.
                                string2RequestBody(String.valueOf(entry.getValue())));
                    }
                        /*if (entry.getValue() instanceof File) {
                            File f = (File) entry.getValue();
                            builder.addFormDataPart(entry.getKey(), f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
                        } else {
                            builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
                        }*/
                }
            }
        }
        return map;
    }

    public static List<MultipartBody.Part> map2Multipart(List<String> params) {
        List<MultipartBody.Part> parts = new ArrayList<>();
        if(params != null && !params.isEmpty()){
            for (String param : params) {
                File file = new File(param);
                if(file.exists()){
                    parts.add(file2Part(file));
                }
            }
        }
        return parts;
    }

    public static List<MultipartBody.Part> map2Multipart(Map<String,String> params) {
        List<MultipartBody.Part> parts = new ArrayList<>();
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (entry.getValue() != null && entry.getValue().length() != 0) {
                    File file = new File(entry.getValue());
                    if (file.exists()) {
                        parts.add(file2Part(entry.getKey(), file));
                    }else{
                        return  null;
                    }
                        /*if (entry.getValue() instanceof File) {
                            File f = (File) entry.getValue();
                            builder.addFormDataPart(entry.getKey(), f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
                        } else {
                            builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
                        }*/
                }
            }
        }
        return parts;
    }
}
